﻿Module modArray
    Public Enum streamreaderResult

        OK = 0
        FileNotFound = -1
        Unknown = -2
    End Enum

    Public Function Array_GetMaxIndex(ByRef xmlArray As xmlInformation()) As Integer

        Dim _idx As Integer
        Dim _max As Integer = 0

        For i As Integer = 0 To (Array_Count(xmlArray) - 1)

            _idx = xmlArray(i).idx

            If _idx > _max Then

                _max = _idx

            End If

        Next

        Return _max
    End Function

    Public Function Array_GetPatchCount(ByRef xmlArray As xmlInformation(), ByVal xmlIndex As String) As Integer

        Dim _idx As String
        Dim _tag As String
        Dim _value As String

        Dim _count As Integer = 0

        xmlIndex = xmlIndex.ToLower

        For i As Integer = 0 To (Array_Count(xmlArray) - 1)

            If Not (xmlArray(i).tag Is Nothing Or xmlArray(i).value Is Nothing) Then

                _idx = xmlArray(i).idx.ToString.ToLower
                _tag = xmlArray(i).tag.ToString.ToLower
                _value = xmlArray(i).value.ToString.ToLower

                If (_idx = xmlIndex) And
                   (_tag = "patch" & _count & "-diff") And
                   (String_IsEmpty(_value) = False) Then

                    _count = _count + 1

                End If

            End If

        Next

        Return _count
    End Function

    Public Function Array_GetIDByValue(ByRef xmlArray As xmlInformation(), ByVal xmlTag As String,
                                       ByVal xmlValue As String) As Integer?

        Dim i As Integer
        Dim _value As String

        If xmlArray Is Nothing Then Return Nothing

        For i = 0 To Array_GetMaxIndex(xmlArray)

            _value = Array_GetValueByTag(xmlArray, i, xmlTag)

            If (_value Is Nothing) Then Return Nothing

            _value = XML_ConvertText(_value, xmlCharConvert.xmlConvertToRead)

            If _value.ToLower = xmlValue.ToLower Then

                Return i

            End If

        Next

        Return Nothing
    End Function

    Public Function Array_GetValueByTag(ByRef xmlArray() As xmlInformation, ByVal xmlIndex As Integer,
                                        ByVal xmlTag As String,
                                        Optional ByVal xmlSubstituteList As List(Of KeyValuePair(Of String, String)) =
                                           Nothing) As String

        Dim i As Integer

        Dim _idx As Integer
        Dim _tag As String
        Dim _value As String

        If xmlArray Is Nothing Then Return Nothing

        xmlIndex = xmlIndex
        xmlTag = xmlTag.ToLower

        For i = 0 To (Array_Count(xmlArray) - 1)

            _idx = xmlArray(i).idx

            _tag = xmlArray(i).tag

            If (_tag IsNot Nothing) Then _tag = _tag.ToString.ToLower

            If (_idx = xmlIndex) And (_tag = xmlTag) Then

                _value = xmlArray(i).value

                If _
                    (_value.Contains("{") = True) And (_value.Contains("}") = True) And
                    Not (xmlSubstituteList Is Nothing) Then

                    _value = XML_ConvertText(_value, xmlCharConvert.xmlConvertToRead)
                    _value = String_ReplaceFromKVP(_value, xmlSubstituteList)

                End If

                Return _value

            End If

        Next

        Return Nothing
    End Function

    Public Function Array_Count(xmlArray As xmlInformation()) As Integer

        Dim _count As Integer

        Try
            _count = xmlArray.Count
        Catch ex As Exception
            _count = 0
        End Try

        Return _count
    End Function

    Public Sub Array_SetValueByTag(ByRef xmlArray As xmlInformation(), ByVal xmlIndex As String, ByVal xmlTag As String,
                                   ByVal xmlValue As String)

        Dim i As Integer

        For i = 0 To (Array_Count(xmlArray) - 1)

            If (xmlArray(i).idx = xmlIndex) And (xmlArray(i).tag.ToLower = xmlTag.ToLower) Then

                xmlValue = XML_ConvertText(xmlValue, xmlCharConvert.xmlConvertToWrite)
                xmlArray(i).value = xmlValue

                Exit Sub

            End If

        Next

        i = (Array_Count(xmlArray))

        ReDim Preserve xmlArray(i)

        xmlArray(i).idx = xmlIndex
        xmlArray(i).tag = xmlTag
        xmlArray(i).value = xmlValue
    End Sub
End Module